﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace BierdopjeTV.Helpers
{
    class HTTPHelper
    {

        public static string GetHTTPResponse(string url)
        {
            System.Diagnostics.Debug.Print("getHTTPResponse :" + url);
            try
            {
                HttpWebRequest _Request = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse _Response = (HttpWebResponse)_Request.GetResponse();

                Stream _DataStream = _Response.GetResponseStream();
                StreamReader _Reader = new StreamReader(_DataStream);
                string responseFromServer = _Reader.ReadToEnd();

                _Reader.Close();
                _DataStream.Close();
                _Response.Close();


                return responseFromServer;
            }
            catch (Exception ex)
            {
                Helpers.Logger.Write(ex.Message, "HTTPHelper.GetHTTPResponse()");                
                return "";
            }
        }

        public static bool DownloadFile(string url, string localfile)
        {            
            
            WebClient Client = new WebClient();            
            try
            {
                Helpers.Logger.Write("Downloading " + localfile, "HTTPHelper.GetHTTPResponse()"); 
                Client.DownloadFile(url, localfile);
                return true;
            }
            catch (Exception ex)
            {
                Helpers.Logger.Write(ex.Message, "HTTPHelper.GetHTTPResponse()"); 
                return false;
            }      
        }
    }
}
